#!/usr/bin/env python
# coding: utf-8

# Create your views here.
from django.contrib import messages

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.views.generic import DetailView, ListView, CreateView, UpdateView
from django.shortcuts import get_object_or_404
from django.core.urlresolvers import reverse
# from models import Computer, Departament, ComputerUser
# from forms import ComputerForm, DepartamentForm, ComputerUserForm
from django.core.context_processors import csrf

from orgcontrol.orgauth.authfuncs import get_user_profile #, get_network_admin, get_structure_admin
from orgcontrol.orgauth.authfuncs import auth_user_required #, network_admin_required, structure_admin_required

from django.utils.decorators import method_decorator

def menu_element(url, name, title=u'', right = False, **kwargs):
    def get_url(url):
        #return reverse(url)
        try: return reverse(url)
        except: return url

    return {'url': get_url(url), 'name': name, 'title': title, 'right': right, 'submenu': kwargs.get('submenu', [])}


def get_default_context(request):

    c = {}
    # messages.info(request, 'Three credits remain in your account.')
    # messages.success(request, 'Profile details updated.')
    # messages.warning(request, 'Your account expires in three days.')
    # messages.error(request, 'Document deleted.')

    c['messages'] = messages.get_messages(request)

    c.update(csrf(request))
    # unblog_submenu = [
    #     menu_element('unblog_topic_list', u'Темы', u'Список тем для разговора'),
    # ]
    prodinf_submenu = [
        menu_element('prodinf/data/knitting', u'Вязальный цех', u'Информация о производстве вязального цеха'),
        menu_element('prodinf/data/sewing', u'Швейный цех', u'Информация о производстве швейного цеха'),
        menu_element('prodinf/data/outlet', u'Выпускной цех', u'Информация о производстве выпускного цеха'),
        menu_element('prodinf/data/sales', u'Реализация', u'Информация о реализации'),
        menu_element('prodinf/report', u'Отчет', u'Отчет о производстве и реализации'),
    ]
    menu = [
        menu_element('home', u'Главная', u'Про все-про-что'),
        menu_element('prodinf/report', u'Производство', u'Информация о производстве и реализации', submenu = prodinf_submenu),
        # menu_element('unblog', u'Записки', u'Про все остальное', submenu = unblog_submenu),
        menu_element('/admin', u'Админка', U'Не для всех', True),
    ]

    user = request.user if request.user.is_authenticated() else None
    profile = get_user_profile(request)
    if not profile: 
        pass
        #menu.append( menu_element('login', 'Войти', True) )
    c['request'] = request
    c['main_menu'] = menu
    if user:
        c['user'] = user
        c['user_profile'] = profile
    return c

def home(request):
    c = get_default_context(request)
    c['title'] = 'Главная'
    return render_to_response("main.html", c )

def process_delete(model_class):
    def wrapper(func):
        pass
        def tmp(*args, **kwargs):
            request = args[0]
            obj_id = request.POST.get('del_id', False)
            if obj_id:
                #model_class.objects.get(pk=obj_id).delete()
                obj = get_object_or_404(model_class, pk=obj_id)
                obj.delete()
                return func(*args, **kwargs)
            return HttpResponseRedirect(request.META['REFERER'])

        return tmp
    return wrapper

# @process_delete(Computer)
# def ComputerDelete(request): return HttpResponseRedirect(reverse('computer_list'))
